﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace GOM_PLAYER
{
    public class process_xml
    {
        XmlDocument doc;

        public process_xml()
        {

        }

        public List<shortcut_keyboard> load_shortcut_keyboard()
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath+"\\general.xml");
            List<shortcut_keyboard> listsk = new List<shortcut_keyboard>();
            shortcut_keyboard sk = new shortcut_keyboard();
            for (int i = 0; i < doc.DocumentElement.ChildNodes[0].ChildNodes[0].ChildNodes.Count; i++)
            {
                sk = new shortcut_keyboard();
                XmlNode node = doc.DocumentElement.ChildNodes[0].ChildNodes[0].ChildNodes[i];
                sk.shortcut_key = node.ChildNodes[0].InnerText;
                sk.description = node.ChildNodes[1].InnerText;
                listsk.Add(sk);
            }
            return listsk;
        }

        public void insert_shortcut_keyboard(shortcut_keyboard sk)
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            //khoi tao cac các Xmlelemment
            XmlElement defined = doc.CreateElement("defined");
            XmlElement shortcutkey = doc.CreateElement("shortcutkey");
            XmlElement description = doc.CreateElement("description");
            //khoi tao cac Xmltext
            XmlText shortcutkeytext = doc.CreateTextNode(sk.shortcut_key);
            XmlText descriptiontext = doc.CreateTextNode(sk.description);
            //them vao cac noi dung
            defined.AppendChild(shortcutkey);
            defined.AppendChild(description);
            //them cac text vao cho cac element
            shortcutkey.AppendChild(shortcutkeytext);
            description.AppendChild(descriptiontext);
            //them vao file XML
            doc.DocumentElement.ChildNodes[0].ChildNodes[0].AppendChild(defined);
            doc.Save(Application.StartupPath + "\\general.xml");
        }

        public void delete_shortcut_keyboard(int index)
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            doc.DocumentElement.ChildNodes[0].ChildNodes[0].RemoveChild(doc.DocumentElement.ChildNodes[0].ChildNodes[0].ChildNodes[index]);
            doc.Save(Application.StartupPath + "\\general.xml");
        }

        public List<shortcut_mouse> load_shortcut_mouse()
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            List<shortcut_mouse> listsk = new List<shortcut_mouse>();
            shortcut_mouse sk = new shortcut_mouse();
            sk = new shortcut_mouse();
            for (int i = 0; i < doc.DocumentElement.ChildNodes[0].ChildNodes[1].ChildNodes.Count; i++)
            {
                XmlNode node = doc.DocumentElement.ChildNodes[0].ChildNodes[1].ChildNodes[i];
                sk.shortcut_key = node.ChildNodes[0].InnerText;
                sk.description = node.ChildNodes[1].InnerText;
                listsk.Add(sk);
            }
            return listsk;
        }
        public void insert_shortcut_mouse(shortcut_mouse sm, int index)
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            //khoi tao cac các Xmlelemment
            XmlElement shortcutkey = doc.CreateElement("shortcutkey");
            XmlElement description = doc.CreateElement("description");
            //khoi tao cac Xmltext
            XmlText shortcutkeytext = doc.CreateTextNode(sm.shortcut_key);
            XmlText descriptiontext = doc.CreateTextNode(sm.description);
            //them cac text vao cho cac element
            shortcutkey.AppendChild(shortcutkeytext);
            description.AppendChild(descriptiontext);
            //them vao file XML
            doc.DocumentElement.ChildNodes[0].ChildNodes[1].ChildNodes[index].RemoveAll();
            doc.DocumentElement.ChildNodes[0].ChildNodes[1].ChildNodes[index].AppendChild(shortcutkey);
            doc.DocumentElement.ChildNodes[0].ChildNodes[1].ChildNodes[index].AppendChild(description);
            doc.Save(Application.StartupPath +"\\general.xml");
        }

        public skin load_skin(string pathfile)
        {
            doc = new XmlDocument();
            doc.Load(pathfile);
            string subpath = "";
            skin sk = new skin();
            XmlNodeList nodelist = doc.DocumentElement.ChildNodes;
            sk.name_skin = nodelist[0].InnerText;
            sk.info = nodelist[1].InnerText;
            sk.theme = new List<theme>();
            sk.file_name = pathfile.Remove(0, pathfile.LastIndexOf("\\", pathfile.Length - 1)).Remove(0, 1);
            for (int j = 0; j < nodelist[2].ChildNodes.Count; j++)
            {
                string filename_theme = nodelist[2].ChildNodes[j].InnerText;
                string name_theme = filename_theme.Substring(0, filename_theme.LastIndexOf("."));
                subpath = pathfile.Substring(0, pathfile.LastIndexOf(sk.file_name));
                sk.theme.Add(new theme(subpath + filename_theme, name_theme));
            }
            sk.play = subpath + nodelist[3].InnerText;
            sk.pause = subpath + nodelist[4].InnerText;
            sk.stop = subpath + nodelist[5].InnerText;
            sk.nextfile = subpath + nodelist[6].InnerText;
            sk.previousfile = subpath + nodelist[7].InnerText;
            sk.volume = subpath + nodelist[8].InnerText;
            sk.fullscreen = subpath + nodelist[9].InnerText;
            sk.controlpanel = subpath + nodelist[10].InnerText;
            sk.playlist = subpath + nodelist[11].InnerText;
            sk.volume_mute = subpath + nodelist[12].InnerText;
            return sk;
        }

        public void insert_skin(string pathfile)
        {
           
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            XmlElement skin=doc.CreateElement("path_skin");
            XmlText skintext=doc.CreateTextNode(pathfile);
            skin.AppendChild(skintext);
            doc.DocumentElement.ChildNodes[1].ChildNodes[1].AppendChild(skin);
            doc.Save(Application.StartupPath + "\\general.xml");
        }

        public void delete_skin(int index)
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            doc.DocumentElement.ChildNodes[1].ChildNodes[1].RemoveChild(doc.DocumentElement.ChildNodes[1].ChildNodes[1].ChildNodes[index]);
            doc.Save(Application.StartupPath + "\\general.xml");
        }

        public List<string> load_skin_inserted()
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            List<string> list_path_skin=new List<string>() ;
            for (int i = 0; i < doc.DocumentElement.ChildNodes[1].ChildNodes[1].ChildNodes.Count; i++)
            {
                if (i == 0)
                    list_path_skin.Add(Application.StartupPath+"\\"+doc.DocumentElement.ChildNodes[1].ChildNodes[1].ChildNodes[i].InnerText);
                else
                    list_path_skin.Add(doc.DocumentElement.ChildNodes[1].ChildNodes[1].ChildNodes[i].InnerText);
            }
            return list_path_skin;
        }

        public void save_playlist(List<song> list_song, string pathfile)
        {
            doc = new XmlDocument();
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement rootNode = doc.CreateElement("list");
            doc.InsertBefore(xmlDeclaration, doc.DocumentElement);
            doc.AppendChild(rootNode);
            for (int i = 0; i < list_song.Count; i++)
            {
                XmlElement song = doc.CreateElement("song");
                rootNode.AppendChild(song);
                XmlElement name_song = doc.CreateElement("name_song");
                XmlElement path_song = doc.CreateElement("path_song");
                XmlText name_song_text = doc.CreateTextNode(list_song[i].name_song);
                XmlText path_song_text = doc.CreateTextNode(list_song[i].path_song);
                name_song.AppendChild(name_song_text);
                path_song.AppendChild(path_song_text);
                song.AppendChild(name_song);
                song.AppendChild(path_song);

            }
            doc.Save(pathfile);
        }

        public List<song> load_playlist(string pathfile)
        {
            List<song> list_song = new List<song>();
            try
            {
                doc = new XmlDocument();
                doc.Load(pathfile);
                song s;
                for (int i = 0; i < doc.DocumentElement.ChildNodes.Count; i++)
                {
                    s = new song();
                    s.name_song = doc.DocumentElement.ChildNodes[i].ChildNodes[0].InnerText;
                    s.path_song = doc.DocumentElement.ChildNodes[i].ChildNodes[1].InnerText;
                    list_song.Add(s);
                }
                return list_song;
            }
            catch(Exception)
            {
                return list_song;
            }
        }
        public void insert_logo(logo lg)
        {

            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            XmlElement logo = doc.CreateElement("logo");
            XmlElement path_logo = doc.CreateElement("path_logo");
            XmlElement name_logo = doc.CreateElement("name_logo");
            XmlText path_logotext = doc.CreateTextNode(lg.path);
            XmlText name_logotext = doc.CreateTextNode(lg.name_logo);
            path_logo.AppendChild(path_logotext);
            name_logo.AppendChild(name_logotext);
            logo.AppendChild(name_logo);
            logo.AppendChild(path_logo);
            doc.DocumentElement.ChildNodes[2].AppendChild(logo);
            doc.Save(Application.StartupPath + "\\general.xml");
        }

        public void delete_logo(int index)
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            doc.DocumentElement.ChildNodes[2].RemoveChild(doc.DocumentElement.ChildNodes[2].ChildNodes[index+1]);
            doc.Save(Application.StartupPath + "\\general.xml");
        }

        public List<logo> load_logo_inserted()
        {
            doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\general.xml");
            List<logo> list_logo = new List<logo>();
            for (int i = 1; i < doc.DocumentElement.ChildNodes[2].ChildNodes.Count; i++)
            {
                if(i==1)
                    list_logo.Add(new logo(Application.StartupPath+"\\"+doc.DocumentElement.ChildNodes[2].ChildNodes[i].ChildNodes[1].InnerText, doc.DocumentElement.ChildNodes[2].ChildNodes[i].ChildNodes[0].InnerText));
                else
                    list_logo.Add( new logo(doc.DocumentElement.ChildNodes[2].ChildNodes[i].ChildNodes[1].InnerText,doc.DocumentElement.ChildNodes[2].ChildNodes[i].ChildNodes[0].InnerText) );
            }
            return list_logo;
        }
        
    }
}